package br.liveo.ndrawer.slice;

import br.liveo.model.HelpLiveo;
import br.liveo.navigationliveo.NavigationLiveo;
import br.liveo.ndrawer.ResourceTable;
import br.liveo.utils.ToastUtil;

import ohos.aafwk.content.Intent;

import ohos.agp.components.*;
import ohos.agp.utils.Color;
import ohos.agp.utils.Point;
import ohos.agp.window.service.DisplayManager;
import ohos.agp.window.service.WindowManager;

import ohos.app.Context;

import java.util.ArrayList;

/**
 * MainAbilitySlice
 */
public class MainAbilitySlice extends NavigationLiveo implements ListContainer.ItemClickedListener {


    @Override
    public void onInt(Intent intent) {
        // 设置状态栏的高度
        getWindow().addFlags(WindowManager.LayoutConfig.MARK_TRANSLUCENT_STATUS);
        if (getHeight(this) > 129) {
            statusBarHeight = getHeight(this) - 126;
        } else {
            statusBarHeight = getHeight(this);
        }
        // User Information
        this.userName.setText("Rudson Lima");
        this.userEmail.setText(this.getString(ResourceTable.String_Email));
        this.userPhoto.setPixelMapAndCircle(ResourceTable.Media_ic_rudsonlive);
        this.userBackground.setPixelMap(ResourceTable.Media_ic_user_background_first);

        mHelpLiveo = new HelpLiveo();
        mHelpLiveo.add("Inbox", ResourceTable.Media_ic_inbox_black_24dp, 7);
        mHelpLiveo.addSubHeader("Categories"); // Item subHeader
        mHelpLiveo.add("Starred", ResourceTable.Media_ic_star_black_24dp);
        mHelpLiveo.add("Sent Mail", ResourceTable.Media_ic_send_black_24dp);
        mHelpLiveo.addNoCheck("Drafts", ResourceTable.Media_ic_drafts_black_24dp);
        mHelpLiveo.addSeparator(); // Item separator
        mHelpLiveo.add("Trash", ResourceTable.Media_ic_delete_black_24dp);
        mHelpLiveo.add("Spam", ResourceTable.Media_ic_report_black_24dp, 120);

        TabList.Tab tab1 = tabList.new Tab(getContext());
        tab1.setText("STARRED");
        tabList.addTab(tab1);
        TabList.Tab tab2 = tabList.new Tab(getContext());
        tab2.setText("IMPORTANT");
        tabList.addTab(tab2);
        TabList.Tab tab3 = tabList.new Tab(getContext());
        tab3.setText("DOCUMENTS");
        tabList.addTab(tab3);

        pages = new ArrayList<>();
        pages.add(create("Starred"));
        pages.add(create("Important"));
        pages.add(create("Documents"));

        with(this).startingPosition(2)
                .colorItemSelected(Color.getIntColor("#2196F3"))
                .setFooter("Settings", ResourceTable.Media_ic_settings_black_24dp)
                .setAddListener(onClickAdd)
                .setUserPhotoListener(onClickPhoto)
                .setOnClickFooter(onClickFooter);

    }

    private Integer getHeight(Context context) {
        Point point = new Point();
        DisplayManager.getInstance().getDefaultDisplay(context).get().getSize(point);
        Point point1 = new Point();
        DisplayManager.getInstance().getDefaultDisplay(context).get().getRealSize(point1);
        return point1.getPointYToInt() - point.getPointYToInt();
    }

    private Component create(String text) {
        final Component rootView = LayoutScatter.getInstance(this)
                .parse(ResourceTable.Layout_page_slider_main, null, false);
        Text t1 = (Text) rootView.findComponentById(ResourceTable.Id_txtTitle);
        t1.setText(text);
        return rootView;
    }


    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public void onItemClicked(ListContainer listContainer, Component component, int i1, long l) {
        switch (i1) {
            case 2:
                pages = new ArrayList<>();
                pages.add(create("Starred"));
                pages.add(create("Important"));
                pages.add(create("Documents"));
                break;

            default:
                pages = new ArrayList<>();
                pages.add(create(mHelpLiveo.get(i1).getName()));
                break;
        }
        setCheckedItemNavigation(i1);
    }

    private Component.ClickedListener onClickAdd = component -> {
        ToastUtil.show(this, "Add");
    };

    private Component.ClickedListener onClickPhoto = component -> {
        ToastUtil.show(this, "onClickPhoto :D");
        closeDrawer();
    };

    private Component.ClickedListener onClickFooter = component -> {
        present(new SettingAbilitySlice(), new Intent());
        closeDrawer();
    };

}
